package com.reagent.system.service.impl;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reagent.response.Result;
import com.reagent.system.entity.Merchant;
import com.reagent.system.entity.vo.CommonResult;
import com.reagent.system.entity.vo.QyeryInfo;
import com.reagent.system.entity.vo.merchant.TableResult;
import com.reagent.system.mapper.MerchantMapper;
import com.reagent.system.mapper.UserMapper;
import com.reagent.system.service.MerchantService;
import com.reagent.utils.FileUtils;
import com.reagent.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class MerchantServiceImpl extends ServiceImpl<MerchantMapper, Merchant> implements MerchantService {
    @Resource
    private MerchantMapper merchantMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Object getShopInfo(Integer pageSize, Integer currentPage, String serachInfo) {
        log.info("进入getShopInfo服务处理...");
        log.info("正在创建新的SqlSession...");
        List<Merchant> list = merchantMapper.selectLists(
                new QyeryInfo()
                        .setSerachInfo(serachInfo)
                        .setCurrentPage((currentPage - 1) * pageSize)
                        .setPageSize(pageSize));
        System.out.println("list = " + list);
        String token = JWTUtils.getCurrentToken();
        log.info("获取Token成功...");
        if (!ObjectUtils.isEmpty(token)) {
            List<TableResult> res = new ArrayList<>();
            //判断查询结果是否为空
            log.info("正在复制数据...");
            if (!ObjectUtils.isEmpty(list)) {
                for (Merchant merchant : list) {
                    TableResult tableResult = new TableResult();
                    BeanUtils.copyProperties(merchant, tableResult);
                    res.add(tableResult);
                }
                CommonResult result = new CommonResult();
                result.setList(list)
                        .setPageTotal(merchantMapper.selectCount(null))
                        .setList(res);
                log.info("正在返回数据...");
                return Result.success().data(result);
            }
            log.warn("当前数据为空...");
            return Result.error().message("当前数据为空!");
        }
        log.warn("Token验证失败..");
        log.warn("用户未登录或登录失效..");
        return Result.error().message("请您先登录!");
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Object addAppy(Merchant merchant, MultipartFile photo1, MultipartFile photo2) throws Exception {
        // 获取token
        String token = JWTUtils.getCurrentToken();
        if (!ObjectUtils.isEmpty(token)) {
            // 验证token
            DecodedJWT jwt = JWTUtils.verifyToken(token);
            // 上传文件
            LinkedList<MultipartFile> list = new LinkedList<>();
            list.add(photo1);
            list.add(photo2);
            String[] strings = FileUtils.FileUploadBatch(list);
            // 判断上传结果
            if (!ObjectUtils.isEmpty(strings)) {
                // 整理数据
                merchant.setSigntiem(new Date())
                        .setStatus(2)
                        .setStatus2(1)
                        .setUid(Integer.parseInt(jwt.getClaim("id").asString()))
                        .setImage1(strings[0])
                        .setImage2(strings[1])
                        .setUname(jwt.getClaim("username").asString());
                // 提交数据库
                log.info("开始提交数据库...");
                int insert = merchantMapper.insert(merchant);
                if (insert > 0) {
                    log.info("申请成功");
                    return Result.success().message("申请成功...");
                }
                log.error("申请失败");
                return Result.error().message("申请失败...");
            }
            log.error("图片上传失败");
            return Result.error().message("图片上传失败...");
        }
        log.error("用户未登录或登录过期");
        return Result.error().message("用户未登录或登录过期...");
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Object updateStatusToSuccess(Integer id) {
        Integer update = merchantMapper.updateStatus1(id);
        if (update > 0) {
            return Result.success().message("通过审核!");
        }
        return Result.error().message("拒绝审核!");
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Object updateStatusToSuccess0(Integer id) {
        Integer update = merchantMapper.updateStatus0(id);
        if (update > 0) {
            return Result.success().message("通过审核!");
        }
        return Result.error().message("拒绝审核!");
    }


}

